<template>
  <div class="login-container">
    <!-- element-ui 表单验证规则的四个条件： -->
    <!-- 1. el-form 组件的 model：指定表单的数据对象 -->
    <!-- 2. el-form 组件的 rules：指定表单的验证规则对象 -->
    <!-- 3. el-form-item 组件的 prop：指定被它包裹的表单项要验证哪个字段 -->
    <!-- 4. 需要在具体的某个表单项身上指定 v-model -->
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.mobile"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        :loading="loading"
        @click.native.prevent="handleLogin"
      >Login</el-button>

      <div class="tips">
        <span style="margin-right: 20px">账号: 13800000002</span>
        <span> 密码: 123456</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [
          { required: true, message: '用户名不能为空', trigger: ['blur', 'change'] },
          // 1 3-9 xxxxxxxxx
          { pattern: /^1[3-9]\d{9}$/, message: '请输入合法手机号', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: ['blur', 'change'] },
          { min: 6, max: 16, message: '密码必须是6-16位的字符', trigger: ['blur', 'change'] }
        ]
      },
      passwordType: 'password',
      redirect: undefined,
      // 控制按钮 loading 的显示与隐藏的数据
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录执行的方法
    handleLogin() {
      // 表单兜底校验
      this.$refs.loginRef.validate(async(valid) => {
        // valid 如果为 true，表示校验通过
        // valid 如果为 false，表示校验不通过
        if (valid) {
          // 校验通过
          // 发起请求
          // 开启 loading
          this.loading = true
          try {
            const res = await login(this.loginForm)
            // console.log(res);
            // 关闭 loading
            this.loading = false
            // console.log(res);
            // console.log('登录接口返回的结果',res);
            // 接口成功
            // 提示用户
            this.$message.success('登录成功')
            // TODO: 存储 token
            // 1. 在 vuex 中存 token
            // 2. 在本地存 token
            this.$store.commit('user/setToken', res.data)
            // 跳转到后台主页
            this.$router.push('/')
          } catch (error) {
            // 接口调用失败
            // 关闭 loading
            this.loading = false
          }

          // login(this.loginForm).then(res => {
          //   // 关闭 loading
          //   this.loading = false
          //   // console.log(res);
          //   // console.log('登录接口返回的结果',res);
          //   // 接口成功
          //   // 提示用户
          //   this.$message.success('登录成功')
          //   // TODO: 存储 token
          //   // 跳转到后台主页
          //   // this.$router.push('/')
          // }).catch(err => {
          //   // 接口调用失败
          //   // 关闭 loading
          //   this.loading = false
          // })
        }
      })
    }
  }
}
</script>

<style lang="scss">
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("~@/assets/common/login.jpg"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .loginBtn {
    background: #407ffe;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
